import plotly.graph_objects as go
import numpy as np

# 人的初始位置，位于正方形中心
positionHuman = (0.00, 0.00)
# 四个鳄鱼的初始位置，分别位于正方形的四个角
positionCrocodile1 = (-500.00, 500.00)  # 左上角
positionCrocodile2 = (500.00, 500.00)  # 右上角
positionCrocodile3 = (-500.00, -500.00)  # 左下角
positionCrocodile4 = (500.00, -500.00)  # 右下角

escapeDegree = 240
# 设定距离阈值，当人到鳄鱼的距离小于此值时，认为人被追上
capture_distance = 10

human_x = [positionHuman[0]]
human_y = [positionHuman[1]]
crocodile1_x = [positionCrocodile1[0]]
crocodile1_y = [positionCrocodile1[1]]
crocodile2_x = [positionCrocodile2[0]]
crocodile2_y = [positionCrocodile2[1]]
crocodile3_x = [positionCrocodile3[0]]
crocodile3_y = [positionCrocodile3[1]]
crocodile4_x = [positionCrocodile4[0]]
crocodile4_y = [positionCrocodile4[1]]

for x in range(100):
    angle = np.radians(escapeDegree)
    new_x = human_x[-1] + 10 * np.cos(angle)
    new_y = human_y[-1] + 10 * np.sin(angle)

    lengthCrocodile1 = np.sqrt((human_x[-1] - positionCrocodile1[0]) ** 2 + (human_y[-1] - positionCrocodile1[1]) ** 2)
    lengthCrocodile2 = np.sqrt((human_x[-1] - positionCrocodile2[0]) ** 2 + (human_y[-1] - positionCrocodile2[1]) ** 2)
    lengthCrocodile3 = np.sqrt((human_x[-1] - positionCrocodile3[0]) ** 2 + (human_y[-1] - positionCrocodile3[1]) ** 2)
    lengthCrocodile4 = np.sqrt((human_x[-1] - positionCrocodile4[0]) ** 2 + (human_y[-1] - positionCrocodile4[1]) ** 2)

    if (x >= 2):
        distances = [lengthCrocodile1, lengthCrocodile2, lengthCrocodile3, lengthCrocodile4]
        min_distance_index = np.argmin(distances)
        if min_distance_index == 0 or min_distance_index == 2:
            escapeDegree = escapeDegree - 20
        else:
            escapeDegree = escapeDegree + 20

    human_x.append(new_x)
    human_y.append(new_y)

    # 左上角鳄鱼移动
    angle1 = np.arctan2(human_y[-1] - positionCrocodile1[1], human_x[-1] - positionCrocodile1[0])
    new_x1 = positionCrocodile1[0] + 20 * np.cos(angle1)
    new_y1 = positionCrocodile1[1] + 20 * np.sin(angle1)
    positionCrocodile1 = (new_x1, new_y1)
    crocodile1_x.append(new_x1)
    crocodile1_y.append(new_y1)

    # 右上角鳄鱼移动
    angle2 = np.arctan2(human_y[-1] - positionCrocodile2[1], human_x[-1] - positionCrocodile2[0])
    new_x2 = positionCrocodile2[0] + 20 * np.cos(angle2)
    new_y2 = positionCrocodile2[1] + 20 * np.sin(angle2)
    positionCrocodile2 = (new_x2, new_y2)
    crocodile2_x.append(new_x2)
    crocodile2_y.append(new_y2)

    # 左下角鳄鱼移动
    angle3 = np.arctan2(human_y[-1] - positionCrocodile3[1], human_x[-1] - positionCrocodile3[0])
    new_x3 = positionCrocodile3[0] + 20 * np.cos(angle3)
    new_y3 = positionCrocodile3[1] + 20 * np.sin(angle3)
    positionCrocodile3 = (new_x3, new_y3)
    crocodile3_x.append(new_x3)
    crocodile3_y.append(new_y3)

    # 右下角鳄鱼移动
    angle4 = np.arctan2(human_y[-1] - positionCrocodile4[1], human_x[-1] - positionCrocodile4[0])
    new_x4 = positionCrocodile4[0] + 15 * np.cos(angle4)
    new_y4 = positionCrocodile4[1] + 15 * np.sin(angle4)
    positionCrocodile4 = (new_x4, new_y4)
    crocodile4_x.append(new_x4)
    crocodile4_y.append(new_y4)

    # 检查是否被追上
    distance_to_crocodile1 = np.sqrt(
        (human_x[-1] - positionCrocodile1[0]) ** 2 + (human_y[-1] - positionCrocodile1[1]) ** 2)
    distance_to_crocodile2 = np.sqrt(
        (human_x[-1] - positionCrocodile2[0]) ** 2 + (human_y[-1] - positionCrocodile2[1]) ** 2)
    distance_to_crocodile3 = np.sqrt(
        (human_x[-1] - positionCrocodile3[0]) ** 2 + (human_y[-1] - positionCrocodile3[1]) ** 2)
    distance_to_crocodile4 = np.sqrt(
        (human_x[-1] - positionCrocodile4[0]) ** 2 + (human_y[-1] - positionCrocodile4[1]) ** 2)
    if distance_to_crocodile1 < capture_distance or distance_to_crocodile2 < capture_distance or distance_to_crocodile3 < capture_distance or distance_to_crocodile4 < capture_distance:
        print("人被鳄鱼追上了，模拟结束！")
        break

# 创建 Plotly 图形
fig = go.Figure()

# 添加人的轨迹
fig.add_trace(go.Scatter(x=human_x, y=human_y, mode='lines+markers', name='人', marker=dict(color='brown')))

# 添加四个鳄鱼的轨迹
fig.add_trace(
    go.Scatter(x=crocodile1_x, y=crocodile1_y, mode='lines+markers', name='左上角鳄鱼', marker=dict(color='green')))
fig.add_trace(
    go.Scatter(x=crocodile2_x, y=crocodile2_y, mode='lines+markers', name='右上角鳄鱼', marker=dict(color='red')))
fig.add_trace(
    go.Scatter(x=crocodile3_x, y=crocodile3_y, mode='lines+markers', name='左下角鳄鱼', marker=dict(color='blue')))
fig.add_trace(
    go.Scatter(x=crocodile4_x, y=crocodile4_y, mode='lines+markers', name='右下角鳄鱼', marker=dict(color='yellow')))

# 设置图形布局
fig.update_layout(
    title='四个鳄鱼追人模拟',
    xaxis_title='X 坐标',
    yaxis_title='Y 坐标',
    showlegend=True
)

# 显示图形
fig.show()
